package com.bricks2break;

import java.util.ArrayList;

import com.sprites.Brick;
import com.sprites.DoubleBallSpeedBrick;
import com.sprites.DoublePointsBrick;

import sheep.graphics.Color;
import android.graphics.Paint;

public final class AbstractBrickFactory {

	/**
	 * Method that make the bricks corresponding to a certain level
	 * 
	 * @param level
	 *            the level - which level to add the bricks to
	 * @param lvlNumber
	 *            the level number - which level to fetch
	 * @return the finished arraylist of bricks
	 */
	public static ArrayList<Brick> makeBricks( int lvlNumber) {
		ArrayList<Brick> bricks = new ArrayList<Brick>();

		// Fetch the level string, calculate number of rows/columns and
		// width/height
		char[] lvl = Statics.bricks[lvlNumber].toCharArray();
		int numberOfCols = Statics.numberOfCols;
		int numberOfRows = (lvl.length) / numberOfCols;
		float width = Statics.image_brick.getWidth() + 1.0f;
		float height = Statics.image_brick.getHeight() + 1.0f;

		int startX = 50;
		float startY = (float) (0.5 * Statics.SCREEN_HEIGHT - 0.5 * height * (numberOfRows - 1));
		Statics.POSITION_BRICK_SPAWN_TOP = startY - Statics.DISTANCE_FROM_CENTER;
		Statics.POSITION_BRICK_SPAWN_BOTTOM = Statics.SCREEN_HEIGHT - startY + Statics.DISTANCE_FROM_CENTER;

		// Create a 2d array of the correct bricks
		for (int i = 0; i < numberOfRows; i++) {
			for (int j = 0; j < numberOfCols; j++) {
				Brick b;
				switch (lvl[i * numberOfCols + j]) {
				case 'w':
					b = new Brick(Statics.white, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'r':
					b = new Brick(Statics.red, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'b':
					b = new Brick(Statics.blue, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'g':
					b = new Brick(Statics.green, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'p':
					b = new Brick(Statics.pink, Statics.NORMAL_BRICK_VALUE);
					break;
				case 't':
					b = new Brick(Statics.turquoise, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'o':
					b = new Brick(Statics.orange, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'y':
					b = new Brick(Statics.yellow, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'e':
					b = new Brick(Statics.black, Statics.NORMAL_BRICK_VALUE);
					break;
				case 's':
					b = new DoubleBallSpeedBrick(Statics.green, Statics.SPECIAL_BRICK_VALUE, Statics.specialBrickTime);
					break;
				case 'q':
					b = new Brick(new Paint(new Color((int) (255 * Math.random()), ((int) (255 * Math.random())), ((int) (255 * Math.random())))), Statics.NORMAL_BRICK_VALUE);
					break;
				case 'l':
					b = new Brick(Statics.lightSkinTone, Statics.NORMAL_BRICK_VALUE);
					break;
				case 'i':
					b = new DoublePointsBrick(Statics.orange, Statics.SPECIAL_BRICK_VALUE, Statics.specialBrickTime);
					break;
				case 'm':
					continue;
				default:
					continue;
				}
				bricks.add(b);
				b.setPositionAndUpdateVariable(startX + width * j, startY + height * i);
				Statics.level.getCollisionLayer().addSprite(b);
			}
		}
		return bricks;
	}
}